<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>esd管理系统</title>
    <!-- 引入样式 -->
    <link rel="stylesheet" href="/backend/plugins/element-ui/index.css">
    <link rel="stylesheet" href="/backend/plugins/font-awesome/css/font-awesome.min.css">
    <link rel="stylesheet" href="/backend/styles/list.css">

</head>
<body class="hold-transition">
<div id="app">
    <div class="content-header">
        <h1>通道类型列表</h1>
        <el-breadcrumb separator-class="el-icon-arrow-right" class="breadcrumb">
            <el-breadcrumb-item>首页</el-breadcrumb-item>
            <el-breadcrumb-item>终端管理</el-breadcrumb-item>
            <el-breadcrumb-item>通道类型列表</el-breadcrumb-item>
        </el-breadcrumb>
    </div>
    <div class="app-container">
        <div class="box">
            <el-row>
                <el-col :span="24">
                    <div style="margin-left: 10px;">
                        <el-button type="primary" icon="el-icon-plus" plain class="butT" @click="handleCreate()">添加</el-button>
                    </div>
                </el-col>
            </el-row>
            <el-table
                    size="small"
                    current-row-key="channelTypeId"
                    :data="dataList"
                    highlight-current-row>
                <el-table-column type="index" align="center" label="序号"></el-table-column>
                <el-table-column prop="channelTypeName" label="通道类型名称" align="center"></el-table-column>
                <el-table-column prop="channelTypeDesc" label="通道类型描述" align="center"></el-table-column>
                <el-table-column label="操作" align="center">
                    <template slot-scope="scope">
                        <el-button type="primary" size="mini" @click="handleUpdate(scope.row)">编辑</el-button>
                        <el-button size="mini" type="danger" @click="handleDelete(scope.row)">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <!-- 新增标签弹层 -->
            <div class="add-form">
                <el-dialog :title="dialogTitle" :visible.sync="dialogFormVisible" center>
                    <el-form ref="dataAddForm" :model="formData" :rules="rules" label-position="right"
                             label-width="120px">
                        <el-row>
                            <el-form-item label="通道类型名称" prop="channelTypeName">
                                <!--<el-input v-model="formData.channelTypeName"/>-->
                                <el-select v-model="formData.channelTypeName" placeholder="请选择" style="width: 250px">
                                    <el-option
                                            v-for="item in channelTypeList"
                                            :key="item.id"
                                            :label="item.channelTypeName"
                                            :value="item.channelTypeName">
                                    </el-option>
                                </el-select>
                            </el-form-item>
                            <el-form-item label="通道类型描述" prop="channelTypeDesc">
                                <el-input
                                        type="textarea"
                                        :rows="3"
                                        placeholder="请输入内容"
                                        v-model="formData.channelTypeDesc">
                                </el-input>
                            </el-form-item>
                        </el-row>
                    </el-form>
                    <div slot="footer" class="dialog-footer">
                        <el-button @click="cancelOpt">取消</el-button>
                        <el-button type="primary" @click="saveOrUpdate">确定</el-button>
                    </div>
                </el-dialog>
            </div>
        </div>
    </div>
</div>
</body>
<!-- 引入组件库 -->
<script src="/backend/plugins/vue/vue.js"></script>
<script src="/backend/plugins/element-ui/index.js"></script>
<script type="text/javascript" src="/backend/plugins/jquery/jquery.min.js"></script>
<script src="/backend/plugins/axios/axios.min.js"></script>
<script src="/backend/js/request.js"></script>
<script>
    var vue = new Vue({
        el: '#app',
        data: {
            dialogTitle: null,//对话框标题
            channelTypeList: [],//通道类型列表
            dataList: [],//当前页要展示的分页列表数据
            formData: {},//表单数据
            dialogFormVisible: false,//增加表单是否可见
            dialogFormVisible4Edit: false,//编辑表单是否可见
            rules: {//校验规则
                channelTypeName: [{required: true, message: '通道类型名称为必填项', trigger: 'blur'},
                    {pattern: '[^ \x22]+', message: '通道类型名称不能为空'}],
                channelTypeDesc: [{required: true, message: '通道类型描述为必填项', trigger: 'blur'},
                    {pattern: '[^ \x22]+', message: '通道类型描述不能为空'}]
            }
        },
        created() {
            this.findAll();
            axios.get('/channelType/getChannelTypes').then((res) => {
                this.channelTypeList = res.data ;
            });
        },
        methods: {
            saveOrUpdate(){
               if(this.dialogTitle === '新增通道类型信息'){
                    this.handleAdd();
               }else if(this.dialogTitle === '编辑通道类型信息'){
                    this.handleEdit();
               }
            },
            //编辑
            handleEdit() {
                //进行表单校验
                this.$refs['dataAddForm'].validate((valid) => {
                    if (valid) {
                        this.formData.channelTypeName = this.formData.channelTypeName.trim();
                        this.formData.channelTypeDesc = this.formData.channelTypeDesc.trim();
                        $axios({
                            url: '/channelType/updateChannelType',
                            method: 'put',
                            data: this.formData
                        }).then((res) => {
                            this.findAll();
                            this.$message.success(res.msg);
                        }).finally(() => {
                            this.findAll();
                            this.dialogFormVisible = false;
                            this.formData = {}
                        });
                    } else {
                        //表单校验不通过
                        this.$message.error("表单数据校验失败!");
                        return false;
                    }
                });
            },
            //添加
            handleAdd() {
                //进行表单校验
                this.$refs['dataAddForm'].validate((valid) => {
                    if (valid) {
                        this.formData.channelTypeName = this.formData.channelTypeName.trim();
                        this.formData.channelTypeDesc = this.formData.channelTypeDesc.trim();
                        $axios({
                            url: '/channelType/addChannelType',
                            method: 'post',
                            data: this.formData
                        }).then((res) => {
                            this.findAll();
                            this.dialogFormVisible = false;
                            this.$message.success(res.msg);
                        });
                    } else {
                        //校验不通过
                        this.$message.error("数据校验失败，请检查你的输入信息是否正确！");
                        return false;
                    }
                });
            },
            //分页查询
            findAll() {
                axios.get("/channelType/listAll").then((res) => {
                    //解析Controller响应回的数据，为模型数据赋值
                    this.dataList = res.data.data;
                });
            },
            // 重置表单
            resetForm() {
                this.formData = {};//重置数据
            },
            // 弹出添加窗口
            handleCreate() {
                this.dialogTitle = '新增通道类型信息' ;
                //弹出新增窗口
                this.dialogFormVisible = true;
                this.resetForm();
            },
            cancelOpt(){
                this.dialogFormVisible = false;
                this.formData = {};
                this.$message("已取消操作！！");
            },
            // 弹出编辑窗口
            handleUpdate(row) {
                this.dialogTitle = '编辑通道类型信息' ;
                //弹出编辑窗口
                this.dialogFormVisible = true;
                $axios({
                    url: '/channelType/getById/' + row.channelTypeId,
                    method: 'get',
                    params: null
                }).then((res) => {
                    this.formData = res.data;
                });
            },
            // 删除
            handleDelete(row) {
                this.$confirm("你确定要删除当前数据吗？", "提示", {//确认框
                    type: 'warning'
                }).then(() => {
                    $axios({
                        url: '/channelType/deleteChannelType/' + row.channelTypeId,
                        method: 'delete',
                        params: null
                    }).then((res) => {
                        this.findAll();
                        if(res.code === 0){
                            this.$message.success("删除成功！");
                        }else{
                            this.$message.error(res.msg);
                        }
                    });
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '操作已取消'
                    });
                });
            }
        }
    })
</script>
</html>